package cc.cc4414.cc02.system.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import cc.cc4414.cc02.system.entity.Resource;

/**
 * @author cc 2018年6月12日
 */
public interface ResourceRepository extends JpaRepository<Resource, Long> {
	List<Resource> findAllByOrderByUrlDesc();

	/**
	 * 将id为resourceId的资源的所需权限设置为空
	 * 
	 * @param resourceId
	 *            资源id
	 */
	@Modifying
	@Query(value = "delete from resource_authority where resource_id = ?", nativeQuery = true)
	void deleteResourceAuthorityByResourceId(Long resourceId);

	/**
	 * 让权限authorityId可以访问资源resourceId
	 * 
	 * @param resourceId
	 *            资源id
	 * @param authorityId
	 *            权限id
	 */
	@Modifying
	@Query(value = "insert into resource_authority (resource_id, authority_id) values (?1, ?2)", nativeQuery = true)
	void saveResourceAuthorityByResourceIdAndAuthorityId(Long resourceId, Long authorityId);
}
